import { IQueryObject } from 'tiny-entity2/queryObject';
import { Transaction } from "tiny-entity2";
import { ApiDataContext } from "../domain/apiDataContext";
import { AuditLog } from "../domain/models/auditLog";
import { BaseService } from "./baseService";

export class AuditLogService extends BaseService {
    CreateAuditLog(entity: AuditLog, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new AuditLog();
            d.ConverToEntity(entity);
            return ctx.Create(d);
        });
    }
    UpdateAuditLog(entity: AuditLog, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new AuditLog();
            d.ConverToEntity(entity);
            return ctx.Update(d);
        });
    }
    GetAuditLogById(id: string) {
        let ctx = new ApiDataContext();
    }
    QueryAuditLog(params: AuditLogQueryParams) {
        let ctx = new ApiDataContext();
        let q = ctx.AuditLog as IQueryObject<AuditLog>;
        if (params) {
            if (params.tmpComItemId) {
                q = q.Where(x => x.tmpComItemId == $args1, { $args1: params.tmpComItemId });
            }
            if (params.dataRegisterId) {
                q = q.Where(x => x.dataRegisterId == $args1, { $args1: params.dataRegisterId });
            }
        }

        return q.OrderByDesc(x => x.createTime).ToList();
    }
}

export interface AuditLogQueryParams {
    tmpComItemId?: string;
    dataRegisterId?: string;
}